package com.lwp.springboot.sbmybatis.config;

import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * app.datasource.first.url=jdbc:mysql://localhost/first
 * app.datasource.first.username=dbuser
 * app.datasource.first.password=dbpass
 * app.datasource.first.configuration.maximum-pool-size=30
 *
 * app.datasource.second.url=jdbc:mysql://localhost/second
 * app.datasource.second.username=dbuser
 * app.datasource.second.password=dbpass
 * app.datasource.second.max-total=30
 */
//@Configuration(proxyBeanMethods = false)
public class MyDataSourcesConfiguration {
    @Bean
    @Primary
    @ConfigurationProperties("app.datasource.first")
    public DataSourceProperties firstDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    @ConfigurationProperties("app.datasource.first.configuration")
    public HikariDataSource firstDataSource(DataSourceProperties firstDataSourceProperties) {
        return firstDataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

    /**
     * 使用common
     * @return
     */
    @Bean
    @ConfigurationProperties("app.datasource.second")
    public BasicDataSource secondDataSource() {
        return DataSourceBuilder.create().type(BasicDataSource.class).build();
    }
}
